% Author: Feng Sheng
% Study: Buysell
% Design: Within-subject; Each subject makes 10*10=100 decisions in a block
% when playing either the role of a buyer or the role of a seller

%% Aim
% Perform advanced statistical analyses on DDM outputs, including
% -Stats on group posteriors
% -Corr. of subject posteriors
% -Corr. of subject biases

%% Setup
clc;
clear;
tic;
root_path = pwd; 
addpath(genpath(root_path));

nrole=2;
nprice=10;
nlottery=10;

role_label={'Buyer','Seller'};
role_color_dark={[235/255 112/255 99/255],[106/255 142/255 208/255]}; 
role_color_median={[238/255 137/255 126/255],[144/255 171/255 220/255]}; 
role_color_light={[243/255 171/255 163/255],[165/255 187/255 227/255]};

bias_label={'Val','Res','Val vs. Res'};
bias_color_median={[195/255 205/255 147/255],[192/255 173/255 203/255]}; 

%% Load data
beh_data=readtable([root_path,'\data\behavior\behavior.csv']);
sub_data_raw=readtable([root_path,'\data\subject\subject.csv']);
sub_data_beh=readtable([root_path,'\data\subject\subject_behavior.csv']);
sub_data_ddm=readtable([root_path,'\data\subject\subject_ddm.csv']);
sub_data_gaze=readtable([root_path,'\data\subject\subject_gaze.csv']);
sub_data_pupil=readtable([root_path,'\data\subject\subject_pupil.csv']);
sub_data_ptm=readtable([root_path,'\data\subject\subject_ptm.csv']);
sub_data_ddm_nointer=readtable([root_path,'\data\subject\subject_ddm_nointer.csv']);

sub_data=[sub_data_raw, sub_data_ptm(:,4:end), sub_data_ddm_nointer(:,end-13:end)];
nsub=height(sub_data);

%%










%% Order

sub_data1=sub_data(sub_data.Role1==1,:);
sub_data2=sub_data(sub_data.Role1==2,:);

%% Probability of choosing lottery
sdata1=sub_data1;
sdata2=sub_data2;

% mean
A=[mean(sdata1.ProbBuy),mean(1-sdata2.ProbSell),mean(1-sdata2.ProbSell)-mean(sdata1.ProbBuy)];
B=[mean(1-sdata1.ProbSell),mean(sdata2.ProbBuy),mean(1-sdata1.ProbSell)-mean(sdata2.ProbBuy)];
C=[mean(1-sdata1.ProbSell-sdata1.ProbBuy),mean(1-sdata2.ProbSell-sdata2.ProbBuy),NaN];
colNames = {'BuyerFirst','SellerFirst','Between'};
rowNames = {'B1','B2','Within'};
disp(array2table([A;B;C],'RowNames',rowNames,'VariableNames',colNames));

% sd
A=[std(sdata1.ProbBuy),std(1-sdata2.ProbSell),NaN];
B=[std(1-sdata1.ProbSell),std(sdata2.ProbBuy),NaN];
C=[std(1-sdata1.ProbSell-sdata1.ProbBuy),std(1-sdata2.ProbSell-sdata2.ProbBuy),NaN];
colNames = {'BuyerFirst','SellerFirst','Between'};
rowNames = {'B1','B2','Within'};
disp(array2table([A;B;C],'RowNames',rowNames,'VariableNames',colNames));

% Within
[h,p,ci,stats]=ttest(1-sdata1.ProbSell-sdata1.ProbBuy,0);
[h,p,ci,stats]=ttest(1-sdata2.ProbSell-sdata2.ProbBuy,0);

% Between
[h,p,ci,stats]=ttest2(1-sdata2.ProbSell,sdata1.ProbBuy);
[h,p,ci,stats]=ttest2(1-sdata1.ProbSell,sdata2.ProbBuy);

% Anova
% buyer
x1(:,1)=sub_data.ProbBuy(:);
x1(:,2)=sub_data.Role1(:);
x1(:,3)=ones(height(sub_data),1);
x1(:,4)=sub_data.Subject(:);
% seller
x2(:,1)=1-sub_data.ProbSell(:);
x2(:,2)=sub_data.Role1(:);
x2(:,3)=ones(height(sub_data),1)*2;
x2(:,4)=sub_data.Subject(:);
%
x=[x1;x2];
mixed_between_within_anova(x,0);

% 2-sample t test
mean(1-sdata2.ProbSell - sdata2.ProbBuy) - mean(1-sdata1.ProbSell - sdata1.ProbBuy);
[h,p,ci,stats]=ttest2(1-sdata2.ProbSell - sdata2.ProbBuy, 1-sdata1.ProbSell - sdata1.ProbBuy);

%% WTP vs. WTA
sdata1=sub_data1;
sdata2=sub_data2;

% mean
A=[mean(sdata1.WTP),mean(sdata2.WTA),mean(sdata2.WTA)-mean(sdata1.WTP)];
B=[mean(sdata1.WTA),mean(sdata2.WTP),mean(sdata1.WTA)-mean(sdata2.WTP)];
C=[mean(sdata1.WTA-sdata1.WTP),mean(sdata2.WTA-sdata2.WTP),NaN];
colNames = {'BuyerFirst','SellerFirst','Between'};
rowNames = {'B1','B2','Within'};
disp(array2table([A;B;C],'RowNames',rowNames,'VariableNames',colNames));

% sd
A=[std(sdata1.WTP),std(sdata2.WTA),std(sdata2.WTA)-std(sdata1.WTP)];
B=[std(sdata1.WTA),std(sdata2.WTP),std(sdata1.WTA)-std(sdata2.WTP)];
C=[std(sdata1.WTA-sdata1.WTP),std(sdata2.WTA-sdata2.WTP),NaN];
colNames = {'BuyerFirst','SellerFirst','Between'};
rowNames = {'B1','B2','Within'};
disp(array2table([A;B;C],'RowNames',rowNames,'VariableNames',colNames));

% Within
[h,p,ci,stats]=ttest(sdata1.WTA-sdata1.WTP,0);
[h,p,ci,stats]=ttest(sdata2.WTA-sdata2.WTP,0);

% Between
[h,p,ci,stats]=ttest2(sdata2.WTA,sdata1.WTP);
[h,p,ci,stats]=ttest2(sdata1.WTA,sdata2.WTP);

% Anova
% buyer
x1(:,1)=sub_data.WTP(:);
x1(:,2)=sub_data.Role1(:);
x1(:,3)=ones(height(sub_data),1);
x1(:,4)=sub_data.Subject(:);
% seller
x2(:,1)=sub_data.WTA(:);
x2(:,2)=sub_data.Role1(:);
x2(:,3)=ones(height(sub_data),1)*2;
x2(:,4)=sub_data.Subject(:);
%
x=[x1;x2];
mixed_between_within_anova(x,0);
% 2-sample t test
mean(sdata2.WTA - sdata2.WTP - sdata1.WTA + sdata1.WTP);
[h,p,ci,stats]=ttest2(sdata2.WTA-sdata2.WTP, sdata1.WTA-sdata1.WTP);

%% RT
sdata1=sub_data1;
sdata2=sub_data2;

[h,p,ci,stats]=ttest(sdata1.RTBuyYes-sdata1.RTBuyNo);
[h,p,ci,stats]=ttest(sdata1.RTSellYes-sdata1.RTSellNo);
[h,p,ci,stats]=ttest(sdata2.RTBuyYes-sdata2.RTBuyNo);
[h,p,ci,stats]=ttest(sdata2.RTSellYes-sdata2.RTSellNo);

% mean
A=[mean(sdata1.RTBuyYes-sdata1.RTBuyNo),mean(sdata2.RTSellNo-sdata2.RTSellYes),mean(sdata1.RTBuyYes-sdata1.RTBuyNo)-mean(sdata2.RTSellNo-sdata2.RTSellYes)];
B=[mean(sdata1.RTSellNo-sdata1.RTSellYes),mean(sdata2.RTBuyYes-sdata2.RTBuyNo),mean(sdata2.RTBuyYes-sdata2.RTBuyNo)-mean(sdata1.RTSellNo-sdata1.RTSellYes)];
C=[mean(sdata1.RTBuyYes-sdata1.RTBuyNo-sdata1.RTSellNo+sdata1.RTSellYes),mean(sdata2.RTBuyYes-sdata2.RTBuyNo-sdata2.RTSellNo+sdata2.RTSellYes),NaN];
colNames = {'BuyerFirst','SellerFirst','Between'};
rowNames = {'B1','B2','Within'};
disp(array2table([A;B;C],'RowNames',rowNames,'VariableNames',colNames));

% sd
A=[std(sdata1.RTBuyYes-sdata1.RTBuyNo),std(sdata2.RTSellNo-sdata2.RTSellYes),std(sdata1.RTBuyYes-sdata1.RTBuyNo)-std(sdata2.RTSellNo-sdata2.RTSellYes)];
B=[std(sdata1.RTSellNo-sdata1.RTSellYes),std(sdata2.RTBuyYes-sdata2.RTBuyNo),std(sdata2.RTBuyYes-sdata2.RTBuyNo)-std(sdata1.RTSellNo-sdata1.RTSellYes)];
C=[std(sdata1.RTBuyYes-sdata1.RTBuyNo-sdata1.RTSellNo+sdata1.RTSellYes),std(sdata2.RTBuyYes-sdata2.RTBuyNo-sdata2.RTSellNo+sdata2.RTSellYes),NaN];
colNames = {'BuyerFirst','SellerFirst','Between'};
rowNames = {'B1','B2','Within'};
disp(array2table([A;B;C],'RowNames',rowNames,'VariableNames',colNames));

% Within
[h,p,ci,stats]=ttest(sdata1.RTBuyYes-sdata1.RTBuyNo-sdata1.RTSellNo+sdata1.RTSellYes,0);
[h,p,ci,stats]=ttest(sdata2.RTBuyYes-sdata2.RTBuyNo-sdata2.RTSellNo+sdata2.RTSellYes,0);

% Between
[h,p,ci,stats]=ttest2(sdata1.RTBuyYes-sdata1.RTBuyNo, sdata2.RTSellNo-sdata2.RTSellYes);
[h,p,ci,stats]=ttest2(sdata2.RTBuyYes-sdata2.RTBuyNo, sdata1.RTSellNo-sdata1.RTSellYes);

% Anova
% buyer
x1(:,1)=sub_data.RTBuyYes(:)-sub_data.RTBuyNo(:);
x1(:,2)=sub_data.Role1(:);
x1(:,3)=ones(height(sub_data),1);
x1(:,4)=sub_data.Subject(:);
% seller
x2(:,1)=sub_data.RTSellNo(:)-sub_data.RTSellYes(:);
x2(:,2)=sub_data.Role1(:);
x2(:,3)=ones(height(sub_data),1)*2;
x2(:,4)=sub_data.Subject(:);
%
x=[x1;x2];
mixed_between_within_anova(x,0);
% 2-sample t test
mean(sdata2.RTBuyYes-sdata2.RTBuyNo-sdata2.RTSellNo+sdata2.RTSellYes) - mean(sdata1.RTBuyYes-sdata1.RTBuyNo-sdata1.RTSellNo+sdata1.RTSellYes);
[h,p,ci,stats]=ttest2(sdata2.RTBuyYes-sdata2.RTBuyNo-sdata2.RTSellNo+sdata2.RTSellYes, sdata1.RTBuyYes-sdata1.RTBuyNo-sdata1.RTSellNo+sdata1.RTSellYes);

%% RTWTP vs. RTWTA
sdata1=sub_data1;
sdata2=sub_data2;

% mean
A=[mean(sdata1.RTWTP),mean(sdata2.RTWTA),mean(sdata2.RTWTA)-mean(sdata1.RTWTP)];
B=[mean(sdata1.RTWTA),mean(sdata2.RTWTP),mean(sdata1.RTWTA)-mean(sdata2.RTWTP)];
C=[mean(sdata1.RTWTA-sdata1.RTWTP),mean(sdata2.RTWTA-sdata2.RTWTP),NaN];
colNames = {'BuyerFirst','SellerFirst','Between'};
rowNames = {'B1','B2','Within'};
disp(array2table([A;B;C],'RowNames',rowNames,'VariableNames',colNames));

% sd
A=[std(sdata1.RTWTP),std(sdata2.RTWTA),std(sdata2.RTWTA)-std(sdata1.RTWTP)];
B=[std(sdata1.RTWTA),std(sdata2.RTWTP),std(sdata1.RTWTA)-std(sdata2.RTWTP)];
C=[std(sdata1.RTWTA-sdata1.RTWTP),std(sdata2.RTWTA-sdata2.RTWTP),NaN];
colNames = {'BuyerFirst','SellerFirst','Between'};
rowNames = {'B1','B2','Within'};
disp(array2table([A;B;C],'RowNames',rowNames,'VariableNames',colNames));

% Within
[h,p,ci,stats]=ttest(sdata1.RTWTA-sdata1.RTWTP,0);
[h,p,ci,stats]=ttest(sdata2.RTWTA-sdata2.RTWTP,0);

% Between
[h,p,ci,stats]=ttest2(sdata2.RTWTA,sdata1.RTWTP);
[h,p,ci,stats]=ttest2(sdata1.RTWTA,sdata2.RTWTP);

% Anova
% buyer
x1(:,1)=sub_data.RTWTP(:);
x1(:,2)=sub_data.Role1(:);
x1(:,3)=ones(height(sub_data),1);
x1(:,4)=sub_data.Subject(:);
% seller
x2(:,1)=sub_data.RTWTA(:);
x2(:,2)=sub_data.Role1(:);
x2(:,3)=ones(height(sub_data),1)*2;
x2(:,4)=sub_data.Subject(:);
%
x=[x1;x2];
mixed_between_within_anova(x,0);
% 2-sample t test
mean(sdata2.RTWTA-sdata2.RTWTP-sdata1.RTWTA+sdata1.RTWTP);
[h,p,ci,stats]=ttest2(sdata2.RTWTA-sdata2.RTWTP, sdata1.RTWTA-sdata1.RTWTP);

%% wLotteryBuy vs. wLotterySell
sdata1=sub_data1;
sdata2=sub_data2;

% mean
A=[mean(sdata1.wLotteryBuy),mean(sdata2.wLotterySell),mean(sdata2.wLotterySell)-mean(sdata1.wLotteryBuy)];
B=[mean(sdata1.wLotterySell),mean(sdata2.wLotteryBuy),mean(sdata1.wLotterySell)-mean(sdata2.wLotteryBuy)];
C=[mean(sdata1.wLotterySell-sdata1.wLotteryBuy),mean(sdata2.wLotterySell-sdata2.wLotteryBuy),NaN];
colNames = {'BuyerFirst','SellerFirst','Between'};
rowNames = {'B1','B2','Within'};
disp(array2table([A;B;C],'RowNames',rowNames,'VariableNames',colNames));

% sd
A=[std(sdata1.wLotteryBuy),std(sdata2.wLotterySell),std(sdata2.wLotterySell)-std(sdata1.wLotteryBuy)];
B=[std(sdata1.wLotterySell),std(sdata2.wLotteryBuy),std(sdata1.wLotterySell)-std(sdata2.wLotteryBuy)];
C=[std(sdata1.wLotterySell-sdata1.wLotteryBuy),std(sdata2.wLotterySell-sdata2.wLotteryBuy),NaN];
colNames = {'BuyerFirst','SellerFirst','Between'};
rowNames = {'B1','B2','Within'};
disp(array2table([A;B;C],'RowNames',rowNames,'VariableNames',colNames));

% Within
[h,p,ci,stats]=ttest(sdata1.wLotterySell-sdata1.wLotteryBuy,0);
[h,p,ci,stats]=ttest(sdata2.wLotterySell-sdata2.wLotteryBuy,0);

% Between
[h,p,ci,stats]=ttest2(sdata2.wLotterySell,sdata1.wLotteryBuy);
[h,p,ci,stats]=ttest2(sdata1.wLotterySell,sdata2.wLotteryBuy);

% Anova
% buyer
x1(:,1)=sub_data.wLotteryBuy(:);
x1(:,2)=sub_data.Role1(:);
x1(:,3)=ones(height(sub_data),1);
x1(:,4)=sub_data.Subject(:);
% seller
x2(:,1)=sub_data.wLotterySell(:);
x2(:,2)=sub_data.Role1(:);
x2(:,3)=ones(height(sub_data),1)*2;
x2(:,4)=sub_data.Subject(:);
%
x=[x1;x2];
mixed_between_within_anova(x,0);
% 2-sample t test
mean(sdata2.wLotterySell-sdata2.wLotteryBuy - sdata1.wLotterySell+sdata1.wLotteryBuy);
[h,p,ci,stats]=ttest2(sdata2.wLotterySell-sdata2.wLotteryBuy, sdata1.wLotterySell-sdata1.wLotteryBuy);

%% zBuy vs. zSell
sdata1=sub_data1;
sdata2=sub_data2;

% mean
A=[mean(sdata1.zBuy),mean(sdata2.zSell),mean(sdata2.zSell)-mean(sdata1.zBuy)];
B=[mean(sdata1.zSell),mean(sdata2.zBuy),mean(sdata1.zSell)-mean(sdata2.zBuy)];
C=[mean(sdata1.zSell-sdata1.zBuy),mean(sdata2.zSell-sdata2.zBuy),NaN];
colNames = {'BuyerFirst','SellerFirst','Between'};
rowNames = {'B1','B2','Within'};
disp(array2table([A;B;C],'RowNames',rowNames,'VariableNames',colNames));

% sd
A=[std(sdata1.zBuy),std(sdata2.zSell),std(sdata2.zSell)-std(sdata1.zBuy)];
B=[std(sdata1.zSell),std(sdata2.zBuy),std(sdata1.zSell)-std(sdata2.zBuy)];
C=[std(sdata1.zSell-sdata1.zBuy),std(sdata2.zSell-sdata2.zBuy),NaN];
colNames = {'BuyerFirst','SellerFirst','Between'};
rowNames = {'B1','B2','Within'};
disp(array2table([A;B;C],'RowNames',rowNames,'VariableNames',colNames));

% Within
[h,p,ci,stats]=ttest(sdata1.zSell-sdata1.zBuy,0);
[h,p,ci,stats]=ttest(sdata2.zSell-sdata2.zBuy,0);

% Between
[h,p,ci,stats]=ttest2(sdata2.zSell,sdata1.zBuy);
[h,p,ci,stats]=ttest2(sdata1.zSell,sdata2.zBuy);

% Anova
% buyer
x1(:,1)=sub_data.zBuy(:);
x1(:,2)=sub_data.Role1(:);
x1(:,3)=ones(height(sub_data),1);
x1(:,4)=sub_data.Subject(:);
% seller
x2(:,1)=sub_data.zSell(:);
x2(:,2)=sub_data.Role1(:);
x2(:,3)=ones(height(sub_data),1)*2;
x2(:,4)=sub_data.Subject(:);
%
x=[x1;x2];
mixed_between_within_anova(x,0);
% 2-sample t test
mean(sdata2.zSell-sdata2.zBuy - sdata1.zSell+sdata1.zBuy);
[h,p,ci,stats]=ttest2(sdata2.zSell-sdata2.zBuy, sdata1.zSell-sdata1.zBuy);

%% vInterceptBuy vs. vInterceptSell
sdata1=sub_data1;
sdata2=sub_data2;

% mean
A=[mean(sdata1.vInterceptBuy),mean(sdata2.vInterceptSell),mean(sdata2.vInterceptSell)-mean(sdata1.vInterceptBuy)];
B=[mean(sdata1.vInterceptSell),mean(sdata2.vInterceptBuy),mean(sdata1.vInterceptSell)-mean(sdata2.vInterceptBuy)];
C=[mean(sdata1.vInterceptSell-sdata1.vInterceptBuy),mean(sdata2.vInterceptSell-sdata2.vInterceptBuy),NaN];
colNames = {'BuyerFirst','SellerFirst','Between'};
rowNames = {'B1','B2','Within'};
disp(array2table([A;B;C],'RowNames',rowNames,'VariableNames',colNames));

% sd
A=[std(sdata1.vInterceptBuy),std(sdata2.vInterceptSell),std(sdata2.vInterceptSell)-std(sdata1.vInterceptBuy)];
B=[std(sdata1.vInterceptSell),std(sdata2.vInterceptBuy),std(sdata1.vInterceptSell)-std(sdata2.vInterceptBuy)];
C=[std(sdata1.vInterceptSell-sdata1.vInterceptBuy),std(sdata2.vInterceptSell-sdata2.vInterceptBuy),NaN];
colNames = {'BuyerFirst','SellerFirst','Between'};
rowNames = {'B1','B2','Within'};
disp(array2table([A;B;C],'RowNames',rowNames,'VariableNames',colNames));

% Within
[h,p,ci,stats]=ttest(sdata1.vInterceptSell-sdata1.vInterceptBuy,0);
[h,p,ci,stats]=ttest(sdata2.vInterceptSell-sdata2.vInterceptBuy,0);

% Between
[h,p,ci,stats]=ttest2(sdata2.vInterceptSell,sdata1.vInterceptBuy);
[h,p,ci,stats]=ttest2(sdata1.vInterceptSell,sdata2.vInterceptBuy);

% Anova
% buyer
x1(:,1)=sub_data.vInterceptBuy(:);
x1(:,2)=sub_data.Role1(:);
x1(:,3)=ones(height(sub_data),1);
x1(:,4)=sub_data.Subject(:);
% seller
x2(:,1)=sub_data.vInterceptSell(:);
x2(:,2)=sub_data.Role1(:);
x2(:,3)=ones(height(sub_data),1)*2;
x2(:,4)=sub_data.Subject(:);
%
x=[x1;x2];
mixed_between_within_anova(x,0);
% 2-sample t test
mean(sdata2.vInterceptSell-sdata2.vInterceptBuy - sdata1.vInterceptSell+sdata1.vInterceptBuy);
[h,p,ci,stats]=ttest2(sdata2.vInterceptSell-sdata2.vInterceptBuy, sdata1.vInterceptSell-sdata1.vInterceptBuy);

%% aBuy vs. aSell
sdata1=sub_data1;
sdata2=sub_data2;

% mean
A=[mean(sdata1.aBuy),mean(sdata2.aSell),mean(sdata2.aSell)-mean(sdata1.aBuy)];
B=[mean(sdata1.aSell),mean(sdata2.aBuy),mean(sdata1.aSell)-mean(sdata2.aBuy)];
C=[mean(sdata1.aSell-sdata1.aBuy),mean(sdata2.aSell-sdata2.aBuy),NaN];
colNames = {'BuyerFirst','SellerFirst','Between'};
rowNames = {'B1','B2','Within'};
disp(array2table([A;B;C],'RowNames',rowNames,'VariableNames',colNames));

% sd
A=[std(sdata1.aBuy),std(sdata2.aSell),std(sdata2.aSell)-std(sdata1.aBuy)];
B=[std(sdata1.aSell),std(sdata2.aBuy),std(sdata1.aSell)-std(sdata2.aBuy)];
C=[std(sdata1.aSell-sdata1.aBuy),std(sdata2.aSell-sdata2.aBuy),NaN];
colNames = {'BuyerFirst','SellerFirst','Between'};
rowNames = {'B1','B2','Within'};
disp(array2table([A;B;C],'RowNames',rowNames,'VariableNames',colNames));

% Within
[h,p,ci,stats]=ttest(sdata1.aSell-sdata1.aBuy,0);
[h,p,ci,stats]=ttest(sdata2.aSell-sdata2.aBuy,0);

% Between
[h,p,ci,stats]=ttest2(sdata2.aSell,sdata1.aBuy);
[h,p,ci,stats]=ttest2(sdata1.aSell,sdata2.aBuy);

% Anova
% buyer
x1(:,1)=sub_data.aBuy(:);
x1(:,2)=sub_data.Role1(:);
x1(:,3)=ones(height(sub_data),1);
x1(:,4)=sub_data.Subject(:);
% seller
x2(:,1)=sub_data.aSell(:);
x2(:,2)=sub_data.Role1(:);
x2(:,3)=ones(height(sub_data),1)*2;
x2(:,4)=sub_data.Subject(:);
%
x=[x1;x2];
mixed_between_within_anova(x,0);
% 2-sample t test
mean(sdata2.aSell-sdata2.aBuy - sdata1.aSell+sdata1.aBuy);
[h,p,ci,stats]=ttest2(sdata2.aSell-sdata2.aBuy, sdata1.aSell-sdata1.aBuy);

%% GazeDurLotteryRatioBuy vs. GazeDurLotteryRatioSell
sdata1=sub_data1;
sdata2=sub_data2;

% mean
A=[mean(sdata1.GazeDurLotteryRatioBuy),mean(sdata2.GazeDurLotteryRatioSell),mean(sdata2.GazeDurLotteryRatioSell)-mean(sdata1.GazeDurLotteryRatioBuy)];
B=[mean(sdata1.GazeDurLotteryRatioSell),mean(sdata2.GazeDurLotteryRatioBuy),mean(sdata1.GazeDurLotteryRatioSell)-mean(sdata2.GazeDurLotteryRatioBuy)];
C=[mean(sdata1.GazeDurLotteryRatioSell-sdata1.GazeDurLotteryRatioBuy),mean(sdata2.GazeDurLotteryRatioSell-sdata2.GazeDurLotteryRatioBuy),NaN];
colNames = {'BuyerFirst','SellerFirst','Between'};
rowNames = {'B1','B2','Within'};
disp(array2table([A;B;C],'RowNames',rowNames,'VariableNames',colNames));

% sd
A=[std(sdata1.GazeDurLotteryRatioBuy),std(sdata2.GazeDurLotteryRatioSell),std(sdata2.GazeDurLotteryRatioSell)-std(sdata1.GazeDurLotteryRatioBuy)];
B=[std(sdata1.GazeDurLotteryRatioSell),std(sdata2.GazeDurLotteryRatioBuy),std(sdata1.GazeDurLotteryRatioSell)-std(sdata2.GazeDurLotteryRatioBuy)];
C=[std(sdata1.GazeDurLotteryRatioSell-sdata1.GazeDurLotteryRatioBuy),std(sdata2.GazeDurLotteryRatioSell-sdata2.GazeDurLotteryRatioBuy),NaN];
colNames = {'BuyerFirst','SellerFirst','Between'};
rowNames = {'B1','B2','Within'};
disp(array2table([A;B;C],'RowNames',rowNames,'VariableNames',colNames));

% Within
[h,p,ci,stats]=ttest(sdata1.GazeDurLotteryRatioSell-sdata1.GazeDurLotteryRatioBuy,0);
[h,p,ci,stats]=ttest(sdata2.GazeDurLotteryRatioSell-sdata2.GazeDurLotteryRatioBuy,0);

% Between
[h,p,ci,stats]=ttest2(sdata2.GazeDurLotteryRatioSell,sdata1.GazeDurLotteryRatioBuy);
[h,p,ci,stats]=ttest2(sdata1.GazeDurLotteryRatioSell,sdata2.GazeDurLotteryRatioBuy);

% Anova
% buyer
x1(:,1)=sub_data.GazeDurLotteryRatioBuy(:);
x1(:,2)=sub_data.Role1(:);
x1(:,3)=ones(height(sub_data),1);
x1(:,4)=sub_data.Subject(:);
% seller
x2(:,1)=sub_data.GazeDurLotteryRatioSell(:);
x2(:,2)=sub_data.Role1(:);
x2(:,3)=ones(height(sub_data),1)*2;
x2(:,4)=sub_data.Subject(:);
%
x=[x1;x2];
mixed_between_within_anova(x,0);
% 2-sample t test
mean(sdata2.GazeDurLotteryRatioSell-sdata2.GazeDurLotteryRatioBuy - sdata1.GazeDurLotteryRatioSell+sdata1.GazeDurLotteryRatioBuy);
[h,p,ci,stats]=ttest2(sdata2.GazeDurLotteryRatioSell-sdata2.GazeDurLotteryRatioBuy, sdata1.GazeDurLotteryRatioSell-sdata1.GazeDurLotteryRatioBuy);

%% FirstGazeLotteryRatioBuy vs. FirstGazeLotteryRatioSell
sdata1=sub_data1;
sdata2=sub_data2;

% mean
A=[mean(sdata1.FirstGazeLotteryRatioBuy),mean(sdata2.FirstGazeLotteryRatioSell),mean(sdata2.FirstGazeLotteryRatioSell)-mean(sdata1.FirstGazeLotteryRatioBuy)];
B=[mean(sdata1.FirstGazeLotteryRatioSell),mean(sdata2.FirstGazeLotteryRatioBuy),mean(sdata1.FirstGazeLotteryRatioSell)-mean(sdata2.FirstGazeLotteryRatioBuy)];
C=[mean(sdata1.FirstGazeLotteryRatioSell-sdata1.FirstGazeLotteryRatioBuy),mean(sdata2.FirstGazeLotteryRatioSell-sdata2.FirstGazeLotteryRatioBuy),NaN];
colNames = {'BuyerFirst','SellerFirst','Between'};
rowNames = {'B1','B2','Within'};
disp(array2table([A;B;C],'RowNames',rowNames,'VariableNames',colNames));

% sd
A=[std(sdata1.FirstGazeLotteryRatioBuy),std(sdata2.FirstGazeLotteryRatioSell),std(sdata2.FirstGazeLotteryRatioSell)-std(sdata1.FirstGazeLotteryRatioBuy)];
B=[std(sdata1.FirstGazeLotteryRatioSell),std(sdata2.FirstGazeLotteryRatioBuy),std(sdata1.FirstGazeLotteryRatioSell)-std(sdata2.FirstGazeLotteryRatioBuy)];
C=[std(sdata1.FirstGazeLotteryRatioSell-sdata1.FirstGazeLotteryRatioBuy),std(sdata2.FirstGazeLotteryRatioSell-sdata2.FirstGazeLotteryRatioBuy),NaN];
colNames = {'BuyerFirst','SellerFirst','Between'};
rowNames = {'B1','B2','Within'};
disp(array2table([A;B;C],'RowNames',rowNames,'VariableNames',colNames));

% Within
[h,p,ci,stats]=ttest(sdata1.FirstGazeLotteryRatioSell-sdata1.FirstGazeLotteryRatioBuy,0);
[h,p,ci,stats]=ttest(sdata2.FirstGazeLotteryRatioSell-sdata2.FirstGazeLotteryRatioBuy,0);

% Between
[h,p,ci,stats]=ttest2(sdata2.FirstGazeLotteryRatioSell,sdata1.FirstGazeLotteryRatioBuy);
[h,p,ci,stats]=ttest2(sdata1.FirstGazeLotteryRatioSell,sdata2.FirstGazeLotteryRatioBuy);

% Anova
% buyer
x1(:,1)=sub_data.FirstGazeLotteryRatioBuy(:);
x1(:,2)=sub_data.Role1(:);
x1(:,3)=ones(height(sub_data),1);
x1(:,4)=sub_data.Subject(:);
% seller
x2(:,1)=sub_data.FirstGazeLotteryRatioSell(:);
x2(:,2)=sub_data.Role1(:);
x2(:,3)=ones(height(sub_data),1)*2;
x2(:,4)=sub_data.Subject(:);
%
x=[x1;x2];
mixed_between_within_anova(x,0);
% 2-sample t test
mean(sdata2.FirstGazeLotteryRatioSell-sdata2.FirstGazeLotteryRatioBuy - sdata1.FirstGazeLotteryRatioSell+sdata1.FirstGazeLotteryRatioBuy);
[h,p,ci,stats]=ttest2(sdata2.FirstGazeLotteryRatioSell-sdata2.FirstGazeLotteryRatioBuy, sdata1.FirstGazeLotteryRatioSell-sdata1.FirstGazeLotteryRatioBuy);

%% DecPupil
sdata1=sub_data1;
sdata2=sub_data2;

% mean
A=[mean(sdata1.DecPupilBuyYes-sdata1.DecPupilBuyNo),mean(sdata2.DecPupilSellNo-sdata2.DecPupilSellYes),mean(sdata1.DecPupilBuyYes-sdata1.DecPupilBuyNo)-mean(sdata2.DecPupilSellNo-sdata2.DecPupilSellYes)];
B=[mean(sdata1.DecPupilSellNo-sdata1.DecPupilSellYes),mean(sdata2.DecPupilBuyYes-sdata2.DecPupilBuyNo),mean(sdata2.DecPupilBuyYes-sdata2.DecPupilBuyNo)-mean(sdata1.DecPupilSellNo-sdata1.DecPupilSellYes)];
C=[mean(sdata1.DecPupilBuyYes-sdata1.DecPupilBuyNo-sdata1.DecPupilSellNo+sdata1.DecPupilSellYes),mean(sdata2.DecPupilBuyYes-sdata2.DecPupilBuyNo-sdata2.DecPupilSellNo+sdata2.DecPupilSellYes),NaN];
colNames = {'BuyerFirst','SellerFirst','Between'};
rowNames = {'B1','B2','Within'};
disp(array2table([A;B;C],'RowNames',rowNames,'VariableNames',colNames));

% sd
A=[std(sdata1.DecPupilBuyYes-sdata1.DecPupilBuyNo),std(sdata2.DecPupilSellNo-sdata2.DecPupilSellYes),std(sdata1.DecPupilBuyYes-sdata1.DecPupilBuyNo)-std(sdata2.DecPupilSellNo-sdata2.DecPupilSellYes)];
B=[std(sdata1.DecPupilSellNo-sdata1.DecPupilSellYes),std(sdata2.DecPupilBuyYes-sdata2.DecPupilBuyNo),std(sdata2.DecPupilBuyYes-sdata2.DecPupilBuyNo)-std(sdata1.DecPupilSellNo-sdata1.DecPupilSellYes)];
C=[std(sdata1.DecPupilBuyYes-sdata1.DecPupilBuyNo-sdata1.DecPupilSellNo+sdata1.DecPupilSellYes),std(sdata2.DecPupilBuyYes-sdata2.DecPupilBuyNo-sdata2.DecPupilSellNo+sdata2.DecPupilSellYes),NaN];
colNames = {'BuyerFirst','SellerFirst','Between'};
rowNames = {'B1','B2','Within'};
disp(array2table([A;B;C],'RowNames',rowNames,'VariableNames',colNames));

% Within
[h,p,ci,stats]=ttest(sdata1.DecPupilBuyYes-sdata1.DecPupilBuyNo-sdata1.DecPupilSellNo+sdata1.DecPupilSellYes,0);
[h,p,ci,stats]=ttest(sdata2.DecPupilBuyYes-sdata2.DecPupilBuyNo-sdata2.DecPupilSellNo+sdata2.DecPupilSellYes,0);

% Between
[h,p,ci,stats]=ttest2(sdata1.DecPupilBuyYes-sdata1.DecPupilBuyNo, sdata2.DecPupilSellNo-sdata2.DecPupilSellYes);
[h,p,ci,stats]=ttest2(sdata2.DecPupilBuyYes-sdata2.DecPupilBuyNo, sdata1.DecPupilSellNo-sdata1.DecPupilSellYes);

% Anova
% buyer
x1(:,1)=sub_data.DecPupilBuyYes(:)-sub_data.DecPupilBuyNo(:);
x1(:,2)=sub_data.Role1(:);
x1(:,3)=ones(height(sub_data),1);
x1(:,4)=sub_data.Subject(:);
% seller
x2(:,1)=sub_data.DecPupilSellNo(:)-sub_data.DecPupilSellYes(:);
x2(:,2)=sub_data.Role1(:);
x2(:,3)=ones(height(sub_data),1)*2;
x2(:,4)=sub_data.Subject(:);
%
x=[x1;x2];
mixed_between_within_anova(x,0);
% 2-sample t test
mean(sdata2.DecPupilBuyYes-sdata2.DecPupilBuyNo-sdata2.DecPupilSellNo+sdata2.DecPupilSellYes - sdata1.DecPupilBuyYes+sdata1.DecPupilBuyNo+sdata1.DecPupilSellNo-sdata1.DecPupilSellYes );
[h,p,ci,stats]=ttest2(sdata2.DecPupilBuyYes-sdata2.DecPupilBuyNo-sdata2.DecPupilSellNo+sdata2.DecPupilSellYes, sdata1.DecPupilBuyYes-sdata1.DecPupilBuyNo-sdata1.DecPupilSellNo+sdata1.DecPupilSellYes);

%% 
